import store from "../store/index.js"
import "./platform.js"



export default function myRequest(api, body, method, mode = 'from') {
	
	
	let platform = this.platformUtil.getPlatForm();
	 //console.log("运行环境："+platform)
	
	let contentType = "";
	if (mode == "json") {
		contentType = "application/json";
		body = JSON.stringify(body)
	} else {
		contentType = "application/x-www-form-urlencoded";
	}
	 //console.log("请求接口"+store.state.baseUrl+api)
	uni.showLoading({
		title:"加载中...",
	})
	return new Promise(function(res, rej) {
		uni.request({
			url: store.state.baseUrl + api,
			method: method || 'POST',
			header: {
				token: store.state.token,
				lat: store.state.lat,
				lng: store.state.lng,
				ip: store.state.ip,
				"Content-Type": contentType,
				platform:platform,
				platform1:store.state.sysInfo.platform
			},
			data: body,
			success:data=> {
				// // //console.log("数据:",data)
				uni.hideLoading();
				console.log("请求接口==",data)
				if(data.statusCode == '200'){
					res(data.data);
				}
				if (data.data.code == 0) {
					res(data.data);
					if(data.data.msg&&data.data.msg!='操作成功'){
						// uni.showToast({
						// 	title:data.data.msg,
						// 	icon:'none'
						// })
					}
					return;
				}
				if (data.data.code == 999) {
					 //console.log("登陆失效")
					store.commit("setToken","")
					
					if(!uni.getAccountInfoSync('token')){
						uni.showModal({
							title:'提示',
							content:'请前往登录',
							showCancel:false,
							success(res) {
								uni.removeStorageSync('token')
								if(res.confirm){
									
								}
							}
						})
					}
					
				}
				if(data.data.code == 500){
					uni.showToast({
						title:data.data.msg,
						icon:'none'
					})
				}
				rej(data);
			},
			fail:err=> {
				uni.hideLoading()
				 //console.log("请求失败=="+JSON.stringify(err))
			}
		})
	})
}
